namespace SimpleWebServices.IO
{
    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Web;
    using Moq;
    using SimpleWebServices;
    using Xunit;

    public sealed class HttpRequestBaseFileFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<HttpRequestBaseFile>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Implements<IDisposable>()
                .Result);
        }

        [Fact]
        public void ctor_HttpRequestBase()
        {
            var mock = new Mock<HttpRequestBase>();
            mock.SetupGet<string>(x => x.HttpMethod).Returns("GET").Verifiable();
            mock.SetupGet<Uri>(x => x.Url).Returns(new Uri("http://example.com/path?query")).Verifiable();
            mock.SetupGet<NameValueCollection>(x => x.Headers).Returns(new NameValueCollection { { "Accept", "*/*" } }).Verifiable();

            using (HttpRequestBaseFile obj = new HttpRequestBaseFile(mock.Object))
            {
                Assert.NotNull(obj);
            }

            mock.VerifyAll();
        }

        [Fact]
        public void ctor_HttpRequestBaseNull()
        {
            Assert.Throws<ArgumentNullException>(() => new HttpRequestBaseFile(null as HttpRequestBase));
        }

        [Fact]
        public void prop_Info()
        {
            var mock = new Mock<HttpRequestBase>();
            mock.SetupGet<string>(x => x.HttpMethod).Returns("GET").Verifiable();
            mock.SetupGet<Uri>(x => x.Url).Returns(new Uri("http://example.com/path?query")).Verifiable();
            mock.SetupGet<NameValueCollection>(x => x.Headers).Returns(new NameValueCollection { { "Accept", "*/*" } }).Verifiable();

            using (HttpRequestBaseFile obj = new HttpRequestBaseFile(mock.Object))
            {
                Assert.NotNull(obj.Info);
            }

            mock.VerifyAll();
        }

        [Fact]
        public void Open()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write("a=b");
                    writer.Flush();
                    stream.Position = 0;

                    var mock = new Mock<HttpRequestBase>();
                    mock.SetupGet<string>(x => x.HttpMethod).Returns("GET").Verifiable();
                    mock.SetupGet<Uri>(x => x.Url).Returns(new Uri("http://example.com/path?query")).Verifiable();
                    mock.SetupGet<NameValueCollection>(x => x.Headers).Returns(new NameValueCollection { { "Accept", "*/*" } }).Verifiable();
                    mock.SetupGet<Stream>(x => x.InputStream).Returns(stream).Verifiable();

                    using (HttpRequestBaseFile obj = new HttpRequestBaseFile(mock.Object))
                    {
                        using (FileStream file = obj.Open())
                        {
                            Assert.NotNull(stream);
                        }
                    }

                    mock.VerifyAll();
                }
            }
        }
    }
}